package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.ucenter.v1.request.AreaFileRequest;
import com.frogs.ucenter.v1.request.AreaShowRequest;
import com.frogs.ucenter.v1.request.PostCodeRequest;
import com.frogs.ucenter.v1.request.SubAreaRequest;
import com.frogs.ucenter.v1.service.AreaInfoService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION+"AreaController")
@RequestMapping("/"+ApiConstants.VERSION)
public class AreaController extends BaseController3 {
	
	@Resource(name=ApiConstants.VERSION + "areaInfoService")
	private AreaInfoService areaInfoService;
	
	private static final String AREA_CODE_EXPORT = "/area/exportJs";
	private static final String AREA_POST_DETAIL = "/area/post/detail" ;
	private static final String AREA_CODE_DETAIL = "/area/detail";
	private static final String AREA_CODE_SUB_LIST = "/area/sub/list";
	
	
	@RequestMapping({AREA_CODE_SUB_LIST})
	public Response areaCodeSubList(HttpServletRequest request) {
		SubAreaRequest areaRequest = new SubAreaRequest(); 
		Response response;
		try {
			areaRequest.parse(request);
			response = areaInfoService.getSubAreaList(areaRequest);
		} catch (Exception e) {
			response =  new FailResponse();
			response.setTip(e.getMessage());
		}
		return response;
	}
	
	@RequestMapping({AREA_CODE_DETAIL})
	public Response areaCodeDetail(HttpServletRequest request) {
		AreaShowRequest areaRequest = new AreaShowRequest(); 
		Response response;
		try {
			areaRequest.parse(request);
			response = areaInfoService.showArea(areaRequest);
		} catch (Exception e) {
			response =  new FailResponse();
			response.setTip(e.getMessage());
		}
		return response;
	}
	
	@RequestMapping({AREA_POST_DETAIL}) 
	public Response areaPostDetail(HttpServletRequest request) {
		PostCodeRequest areaRequest = new PostCodeRequest(); 
		Response response;
		try {
			areaRequest.parse(request);
			response = areaInfoService.getPostCode(areaRequest);
		} catch (Exception e) {
			response =  new FailResponse();
			response.setTip(e.getMessage());
		}
		return response;
	}
	
	@RequestMapping(AREA_CODE_EXPORT)
	public Response areaCodeExportJs(HttpServletRequest request){
		AreaFileRequest areaRequest = new AreaFileRequest();
		areaRequest.parse(request);
		Response response;
		
		try {
			response = areaInfoService.createAreaFile(areaRequest,request);
			response.setTip("生成静态文件成功！");
		} catch (Exception e) {
			response =  new FailResponse();
			response.setTip(e.getMessage());
		}
		
		return response;
		
	}
}
